package com.hst.web.base.proxy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.hst.core.Page;
import com.hst.core.ServiceContext;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.meta.proxies.DefaultDataProxy;
import com.hst.web.auth.po.User;
import com.hst.web.base.service.UserRoleService;
import com.hst.web.sys.entity.TRole;
import com.hst.web.sys.entity.TUserContractor;

public class EmployeeOperateProxy extends DefaultDataProxy{
	
	@Autowired
	IORDao dao;
	
	@Autowired
	ISQLDao sql;
	
	@Autowired
	UserRoleService us;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Page<?> list(Map<String, ?> query, int pageIdx) {
		Map<String,Object> map = new HashMap<String, Object>();
		map = (Map<String, Object>) query;
		User u = (User) ServiceContext.getInstance().getUser();
		TRole role = us.userRole(u.getId());
		if("9".equals(role.getRoleLevel())) {
		}else if("2".equals(role.getRoleLevel())){
			map.put("companyCode", u.getCompanyCode());
		}else if("1".equals(role.getRoleLevel())) {
			List<ORQuery> orqs = new ArrayList<ORQuery>();
			orqs.add(new ORQuery(Op.eq,"userid",u.getUserid()));
			orqs.add(new ORQuery(Op.eq,"companyCode",u.getCompanyCode()));
			List<TUserContractor> contrs = dao.list(TUserContractor.class,orqs);
			List<String> list1 = new ArrayList<String>();
			for(TUserContractor cont:contrs) {
				list1.add(cont.getContractorCode());
			}
			if(list1.size() > 0) {
				map.put("companyCode", u.getCompanyCode());
				map.put("contractorCode1", list1);
			}else {
				return new Page();
			}
		}
		return sql.list("employee.list-employee-operate", map, pageIdx,Page.getPageSize());
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<?> list(Map<String, ?> query) {
		Map<String,Object> map = new HashMap<String, Object>();
		map = (Map<String, Object>) query;
		User u = (User) ServiceContext.getInstance().getUser();
		TRole role = us.userRole(u.getId());
		if("9".equals(role.getRoleLevel())) {
		}else if("2".equals(role.getRoleLevel())){
			map.put("companyCode", u.getCompanyCode());
		}else if("1".equals(role.getRoleLevel())) {
			List<ORQuery> orqs = new ArrayList<ORQuery>();
			orqs.add(new ORQuery(Op.eq,"userid",u.getUserid()));
			orqs.add(new ORQuery(Op.eq,"companyCode",u.getCompanyCode()));
			List<TUserContractor> contrs = dao.list(TUserContractor.class,orqs);
			List<String> list1 = new ArrayList<String>();
			for(TUserContractor cont:contrs) {
				list1.add(cont.getContractorCode());
			}
			if(list1.size() > 0) {
				map.put("companyCode", u.getCompanyCode());
				map.put("contractorCode1", list1);
			}else {
				return new ArrayList();
			}
		}
		return sql.query("employee.list-employee-operate", map);
	}
}
